  import { request } from "./request";
  
		export function getUserInfo(){
			//先判断是否有token 没有执行登录方法
			//如果有token 就通过token获取用户基本信息
			let   token = getApp().globalData.token;
			let userInfo=null;
			if(token==null||token==""){
				  userInfo=login();
			 
			}else{
				userInfo=getUser();
			}
			 
			 getApp().globalData.userInfo=userInfo;
		}
  
  	    export function login() {
		   
		   
 		   uni.login({
		     provider: 'weixin', //使用微信登录
		     success: function (loginRes) {
		       console.log(loginRes.code);
			   
			   let code=loginRes.code;
			   let res = request({
			   
			   	url: '/app/user/registerAndLogin?code='+code
			   
			   });
			   res.then(userInfo => {
			    console.log(userInfo);
			    	uni.showToast({ title: '登录成功！', icon: 'none' });
 				getApp().globalData.userInfo=userInfo;				
				console.log("user-token"+userInfo);
			    uni.setStorageSync('user-token', userInfo.token);

			     uni.redirectTo({
			    	url: '/pages/chat/chat'
			     });
			   }).catch(error => {
			   	console.log("catch" + error.code)
			   });
			   
		     },fail(ff) {
		     	console.log(ff)
		     }
		   });
	   }
	   
	   function getUser(){
		   let res = request({
		   
		   	url: '/app/user/getUserInfo'
		   
		   });
		   
		   res.then(success => {
		      console.log(success);
				return success;
		 
		     }) 
		     
		   
	   }

 